/*
 * Date          Author     Note
 * 2014.11.24    舞者无罪         系统维护
 * 2015.06.24    wzwz       增加返回按钮
 * 2015.06.26    wzwz       读取旧规格参数
 * 2015.07.01    wzwz       旧规格参数读取成功后发到下位机
 * 2015.07.03    wzwz       读完保存为新参数文件
 */
#include <stdio.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "maint.h"
#include "com.h"
#include "update.h"
#include "version.h"
#include "time_set.h"
#include "tscal.h"
#include "info.h"

#define MAINT_CAPTION_LEFT    (30)
#define MAINT_CAPTION_TOP     (60)
#define MAINT_CAPTION_WIDTH   (200)
#define MAINT_CAPTION_HEIGHT  (30)
/*
 *
 */
static void CreateMaintInterface(HWND hWnd)
{
    CreateWindow (MH_BTN_NAME,
        MW_UPDATE1_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_UPDATE,
        MAINT_CAPTION_LEFT, MAINT_CAPTION_TOP, MAINT_CAPTION_WIDTH, MAINT_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_VERSION2_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_VERSION,
        MAINT_CAPTION_LEFT, MAINT_CAPTION_TOP + MAINT_CAPTION_HEIGHT,
        MAINT_CAPTION_WIDTH, MAINT_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_TIME3_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_TIME,
        MAINT_CAPTION_LEFT, MAINT_CAPTION_TOP + MAINT_CAPTION_HEIGHT * 2,
        MAINT_CAPTION_WIDTH, MAINT_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_TOUCHSCREEN4_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_TOUCH_SCREEN,
        MAINT_CAPTION_LEFT, MAINT_CAPTION_TOP + MAINT_CAPTION_HEIGHT * 3,
        MAINT_CAPTION_WIDTH, MAINT_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        GetString(804),
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_READ_PARAM,
        30,
        60 + 30 * 4,
        200,
        30,
        hWnd, 0);
    char esc[64] = {0};
    sprintf(esc, GetString(802), 6);
    CreateWindow(MH_BTN_NAME,
        esc,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_ESC,
        30,
        60 + 30 * 5,
        200,
        30,
        hWnd, 0);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    switch(nID)
    {
    case ID_UPDATE:
        {
            CreateUpdateWindow(hWnd);
        }
        break;
    case ID_VERSION:
        {
            CreateVersionWindow(hWnd);
        }
        break;
    case ID_TIME:
        {
            CreateTimeWindow(hWnd);
        }
        break;
    case ID_TOUCH_SCREEN:
        {
            CreateTscalWindow(hWnd);
        }
        break;
    case ID_READ_PARAM:
        if(STATE_STOP != GetRunState(g_knit_data))
        {
            //
        }
        else if(HasUsb())
        {
            char file[MH_FILE_NAME_MAX_LEN] = { 0 };
            strcat(file, GetUsbDir());
            char sc[64] = {0};
            sprintf(sc, "%u", GetMacNum(g_knit_data));
            strcat(file, sc);
            strcat(file, "/");
            printf("%s\n", file);
            TurnOnMachine(g_knit_data, FALSE);
            int ret = ReadOldParamFile(file, g_knit_data);
            TurnOnMachine(g_knit_data, TRUE);
            if(ret)
            {
                AfxInfoBox(hWnd, NULL, GetString(806));
            }
            else if(WriteMacFile(MAC_PARAM_FILE, g_knit_data))
            {
                AfxInfoBox(hWnd, NULL, GetString(806));
            }
            else
            {
                SendSysParam(g_knit_data);
                SendWorkParam(g_knit_data);
                SetWhichNeedDo(g_knit_data, NEED_DO_RESET_START);
                AfxInfoBox(hWnd, NULL, GetString(805));
            }
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(555));
        }
        break;
    case ID_ESC:
        {
            PostMessage (hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    }
}
/*
 *
 */
static void OnMoveFocus(HWND hWnd, int up)
{
    ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
    switch(nID)
    {
    case ID_UPDATE:
        if(up)
        {
            //
        }
        else
        {
            nID = ID_VERSION;
        }
        break;
    case ID_VERSION:
        if(up)
        {
            nID = ID_UPDATE;
        }
        else
        {
            nID = ID_TIME;
        }
        break;
    case ID_TIME:
        if(up)
        {
            nID = ID_VERSION;
        }
        else
        {
            nID = ID_TOUCH_SCREEN;
        }
        break;
    case ID_TOUCH_SCREEN:
        if(up)
        {
            nID = ID_TIME;
        }
        else
        {
            nID = ID_READ_PARAM;
        }
        break;
    case ID_READ_PARAM:
        if(up)
        {
            nID = ID_TOUCH_SCREEN;
        }
        else
        {
            nID = ID_ESC;
        }
        break;
    case ID_ESC:
        if(up)
        {
            nID = ID_READ_PARAM;
        }
        break;
    }
    SetFocusChild(GetDlgItem(hWnd, nID));
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE || key == SCANCODE_6)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_1)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_UPDATE));
        OnBtnCmd(hWnd, ID_UPDATE);
    }
    else if(key == SCANCODE_2)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_VERSION));
        OnBtnCmd(hWnd, ID_VERSION);
    }
    else if(key == SCANCODE_3)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_TIME));
        OnBtnCmd(hWnd, ID_TIME);
    }
    else if(key == SCANCODE_4)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_TOUCH_SCREEN));
        OnBtnCmd(hWnd, ID_TOUCH_SCREEN);
    }

    else if(key == SCANCODE_5)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_READ_PARAM));
        OnBtnCmd(hWnd, ID_READ_PARAM);
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        OnMoveFocus(hWnd, FALSE);
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        OnMoveFocus(hWnd, TRUE);
    }
    else if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, GetDlgCtrlID(GetFocusChild(hWnd)));
    }
}
/*
 *
 */
static int MaintWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateMaintInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 100;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        DestroyMainWindow(hWnd);
        MainWindowCleanup (hWnd);
        return 0;
    case MSG_DESTROY:
        ShowWindow(hWnd, SW_HIDE);
        DestroyAllControls(hWnd);
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_MAST]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateMaintWindow(HWND hHosting)
{
    int x = (CX_SCREEN - MASTER_WND_CX) / 2;
    int y = (CY_SCREEN - MASTER_WND_CY) / 2;
    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_MAINT_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = MaintWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + MASTER_WND_CX;
    CreateInfo.by = y + MASTER_WND_CY;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_UPDATE));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
